package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.FriendDao;
import com.njtech.pet.entity.Friend;
import com.njtech.pet.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-18 15:26
 * Description FriendDao测试类
 */
public class FriendDaoExample {
    static FriendDao friendDao;
    static Scanner scanner;

    @Before
    public void prepare() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        friendDao = (FriendDao) context.getBean("friendDao");
        System.out.println("准备开始测试" + friendDao.toString());
    }

    @Test
    public void insert() {
        User user = new User();
        user.setId(1);
        User user1 = new User();
        user1.setId(3);
        Friend friend = new Friend(null, user, user1, Constant.Friend.STATE_REJECTED.getValue(), null, null);
        System.out.println("test inserting...");
        friendDao.insert(friend);
    }

    @Test
    public void update() {
        System.out.println("friend id:");
        int id = 1;
        Friend friend = new Friend();
        friend.setId(id);
        friend.setState(Constant.Friend.STATE_ACCEPTED.getValue());
        friendDao.updateByFriendId(friend);
        System.out.println("updated");
    }

    @Test
    public void select() {
        System.out.println("friend id:");
        Integer id = 3;
        User user = new User();
        user.setId(id);
        List<Friend> friends;
        friends = friendDao.selectByUser(user, Constant.Friend.STATE_ACCEPTED.getValue(),null,null,"friendName asc");
        for (Friend temp : friends) {
            System.out.println(temp);
        }
    }

    @Test
    public void testSelectByFriend() {
        User user = new User();
        user.setId(3);
        List<Friend> friends = friendDao.selectByFriend(user, Constant.Friend.STATE_REJECTED.getValue(), null, null, "userName ASC");
        for (Friend temp :
                friends) {
            System.out.println(temp.user);
        }
    }

    @Test
    public void selectByUidAndFidAndState() {
        Integer uid = 1;
        Integer fid = 3;
        String state = Constant.Friend.STATE_REJECTED.getValue();
        List<Friend> friends = friendDao.selectByUidAndFidAndState(uid, fid, state);
        for (Friend friend :
                friends) {
            System.out.println(friend);
        }
    }
}
